#define _GNU_SOURCE 

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <sys/param.h>
#include "config/config.h"




char *
chemin_correct(char* path_www)//, char *path_demande){
{
  char *path = malloc(sizeof(char)*(strlen(ROOT)+ strlen(path_www)+ 1));
  strcpy(path, ROOT);

  strcat(path,path_www);

  char path_demande[BUFFER_SIZE];
  char *res= realpath (path, path_demande);

  if (res == NULL)
    {
      char *index;
      if ((index = strstr(path_www,"index.html")) || (index = strstr(path_www,"index.htm")))
	{
	  path[strlen(path) - strlen(index)]= '\0';
	  printf("path = %s\n",path);
	  
	  
	  res= realpath (path, path_demande);
	  printf("res = %s\n",res);
	}


    }
  if (res == NULL)
    {
      char path_demande2[BUFFER_SIZE];
      path = realloc(path, sizeof(char)*(strlen(path)+strlen(".cpp") +1));
      strcat(path,".cpp");
      
      res = realpath(path, path_demande2); //au cas ou on demande un fichier qui soit un .html.cpp
    }

  free(path);
  path = NULL;
  if (res == NULL)
    return "\0";
  return res;
}


/*int
main(int argc,char* argv[])
{
  char www[]="/";
  char *res=  chemin_correct(www);
  
  printf("le chemin demandé est %s\n",res);
  return EXIT_SUCCESS;
  }*/
